package com.linian.pushforcampus.util;


import com.linian.pushforcampus.service.PushingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * 推送类
 * Author:木芒果
 */

@Component
public class HttpUtil {
    /**
     * 定时服务中，get请求的通用方法，用于获取请求api返回的json数据格式String
     *
     * @param url
     * @return
     */


    public static String getHttpJsonByUrl(String url) {
        try {
            URL urlObj = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setConnectTimeout(60000);
            connection.setReadTimeout(60000);

            int status = connection.getResponseCode();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer content = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();
            connection.disconnect();

            return content.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通用post请求
     *
     * @param url
     * @param json
     * @return
     * @throws Exception
     */
    public static String sendPostRequest(String url, String json) throws Exception {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.setDoOutput(true);
        byte[] out = json.getBytes(StandardCharsets.UTF_8);
        con.getOutputStream().write(out);
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
        String inputLine;
        StringBuilder response = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response.toString();
    }

    public static String sendGetHearderRequest(String url, String token) throws IOException {

        // 创建URL对象
        URL apiUrl = new URL(url);

        // 创建HttpURLConnection对象并设置请求头
        HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Host", "stuworkapp.sandau.edu.cn");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("X-Id-Token", token);

        // 发送请求并获取响应
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            // 关闭连接
            connection.disconnect();
            return response.toString();

        }

        return "success";
    }

    public static String sendActApplyRequest(String url, String json, String token) throws Exception {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("X-Id-Token", token);
        con.setDoOutput(true);
        byte[] out = json.getBytes(StandardCharsets.UTF_8);
        con.getOutputStream().write(out);

        BufferedReader in;
        if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
            in = new BufferedReader(new InputStreamReader(con.getErrorStream(), StandardCharsets.UTF_8));
        } else {
            in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
        }

        String inputLine;
        StringBuilder response = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        return response.toString();
    }


}
